const mysql = require('mysql')

//mysql连接信息配置
let pool = null;

switch (process.env.NODE_ENV) {
	case 'production':
		 pool = mysql.createPool({
		 	host:"localhost",
			user:"dg",
			password:"6653145",
			database:"digitalenergy",
			port: 3306
		})
	break;
	case 'dev': 
		 pool = mysql.createPool({
			host:"localhost",
			user:"root",
			password:"6653145",
			database:"digitalenergy",
			port: 8889
		})
	break;
	default: 
	   return ;
}

let query = function(sql,values) {
	return new Promise((resolve, reject)=>{
		pool.getConnection(function(err, connection){
			if (err) {
				reject(err)
			} else {
				connection.query(sql, values, (err, doc)=>{
					if(err) {
						reject(err)
					} else {
						resolve(doc)
					}
					connection.release()
				})
			}
		})
	})
}

module.exports = query